package com.unknownman.block.job;

import com.unknownman.block.service.TContractLogsService;
import com.unknownman.block.service.TTokenErc20Service;
import lombok.extern.slf4j.Slf4j;
import org.cloud.core.redis.RedisUtil;
import org.cloud.scheduler.constants.MisfireEnum;
import org.cloud.scheduler.job.BaseQuartzJobBean;
import org.jetbrains.annotations.NotNull;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * ERC20和ERC721通证转账处理任务
 */
@Slf4j
public class TokenErc20HourHoldJob extends BaseQuartzJobBean {

    @Autowired
    private TTokenErc20Service TokenErc20Service;

    @Autowired
    RedisUtil redisUtil;

    @Override
    protected void init() {
        this.jobName = "按小时保存当前erc20的持币地址的总数和持有量的总数";
        jobData.put("description", "按小时保存当前erc20的持币地址的总数和持有量的总数到mongoDb中");
        this.jobTime = "0 55 0-23 * * ?";
        setMisfire(MisfireEnum.CronScheduleMisfireEnum.MISFIRE_INSTRUCTION_DO_NOTHING);
    }

    @Override
    protected void executeInternal(@NotNull JobExecutionContext jobExecutionContext) throws JobExecutionException {

        final String redisLockerName = "TokenErc20HourHoldJob.locker";
        if (!redisUtil.getLock(redisLockerName, 60 * 60 * 1000)) {
            return;
        }
        try {
            TokenErc20Service.saveTokenErc20HourHold();
        } finally {
            redisUtil.releaseLock(redisLockerName);
        }
    }
}